package com.example.smart_video.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.wink_172.library.R;
import com.wink_172.library.utils.DisplayUtil;

public
/**
 倒计时控件
 可以自己定义样式   圆形或者矩形 啥都行
 请不要清空中间文字  可以拿view遮挡住即可

 使用方法
 android:id="@+id/circle_time_view"

 binding!!.circleTimeView.setCallback(object : CircleTimeView.ICallback {
 override fun onSendEvent(event: Int, vararg args: Any?) {
 if (event == Constants.EVENT_01) {
 }
 }
 })

 binding!!.circleTimeView.startCountDown(15)
 */
class CustomCircleTimeView extends View {
    //圆轮颜色
    private int mRingColor;
    //圆轮宽度
    private float mRingWidth;
    //圆轮进度值文本大小
    private int mTextSize;
    //宽度
    private int mWidth;
    //高度
    private int mHeight;
    private Paint mPaint;
    //圆环的矩形区域
    private RectF mRectF;


    private int mTextColor;
    private int mAllDay = 0;//总时间
    private float mCurrentDay;


    public void setCallback(ICallback callback) {
        this.mCallback = callback;
    }

    public ICallback mCallback;

    public interface ICallback {
        void onSendEvent(int event, Object... args);
    }


//使用方法
//          (binding!!.circleTimeView as CircleTimeView).setCallback(object : CircleTimeView.ICallback {
//        override fun onSendEvent(event: Int, vararg args: Any) {
//            if (event == Constants.LEFT_BTN) {
//                finish()
//            }
//        }
//    })


    public CustomCircleTimeView(Context context) {
        this(context, null);
    }

    public CustomCircleTimeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomCircleTimeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mRingColor = context.getResources().getColor(R.color.logcat_level_info_color);
        mRingWidth = 3;
        mTextSize = DisplayUtil.sp2px(context, 14);
        mTextColor = context.getResources().getColor(R.color.logcat_level_error_color);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);

        this.setWillNotDraw(false);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
        mRectF = new RectF(0 + mRingWidth / 2, 0 + mRingWidth / 2,
                mWidth - mRingWidth / 2, mHeight - mRingWidth / 2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //颜色
        mPaint.setColor(mRingColor);
        //空心
        mPaint.setStyle(Paint.Style.STROKE);
        //宽度
        mPaint.setStrokeWidth(mRingWidth);
        canvas.drawArc(mRectF, -90, mCurrentDay * 360f / mAllDay, false, mPaint);//圆形
//        (mCurrentDay / 360f * mAllDay)
//        canvas.drawRoundRect(mRectF, 20, 60, mPaint);//矩形

        //绘制文本
        Paint textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setTextAlign(Paint.Align.CENTER);
        String text = (int) mCurrentDay + "天";
        textPaint.setTextSize(mTextSize);
        textPaint.setColor(mTextColor);

        //文字居中显示
        Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
        int baseline = (int) ((mRectF.bottom + mRectF.top - fontMetrics.bottom - fontMetrics.top) / 2);

        canvas.drawText(text, mRectF.centerX(), baseline - 5, textPaint);

        textPaint.setTextSize(20);
        canvas.drawText(content, mRectF.centerX(),
                baseline + 30, textPaint);


    }


    private static final String TAG = "CustomCircleTimeView";

    int mode = -1;//模式
    String content="";

    /**
     * 开始倒计时
     */
    public void startCountDown(int mode, final int all_day, int current_day,String text) {
//        Log.e(TAG, "startCountDown: ====>>" + all_day);
        this.mode = mode;
        mAllDay = all_day;
        mCurrentDay = current_day;
        this.content=text;
        invalidate();

    }


}